﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI
{
    public partial class TextBoxComplete : UserControl
    {
        private DataTable datasource = new DataTable();
        public DataTable DataSource
        {
            get
            {
                return datasource;
            }
            set
            {
                //frm.Location = new Point(this.Parent.Location.X + this.Location.X, this.Parent.Location.Y + this.Location.Y);
                //frm.DesktopLocation = new Point(200, 500);
                //frm.Location = new Point(0, 0);                
                datasource = value;
                CreateDataGrid();
                frm.Controls.Clear();
                frm.Controls.Add(gr);
            }
        }

        private bool DropDown
        {
            set
            {
                if ((bool)value == true && IsShow == false)
                {
                    frm.Show();
                    IsShow = true;
                }
                if ((bool)value == false)
                {
                    frm.Hide();
                    IsShow = false;
                }

            }
        }
        private object value;
        public object Value
        {
            get { return value; }
            set { this.value = value; }
        }

        private int _height = 200;
        public int height { get { return _height; } set { _height = value; frm.Height = _height - 50; } }

        public string StringBrowse { get; set; }
        //private string fieldsearch = "";
        public string FieldSearch { get; set; }
        public string Id { get; set; }
        public int RowShow { get; set; }
        private DataGridView gr = new DataGridView();
        private Form frm = new Form();
        private bool IsShow = false;
        private bool IsChange = true;
        public TextBoxComplete()
        {
            InitializeComponent();
            frm.ControlBox = false;
            frm.FormBorderStyle = FormBorderStyle.None;
            frm.TopMost = true;
            RowShow = 5;
            ////
            this.Width = 100;
            this.CausesValidation = true;
            this.AutoValidate = AutoValidate.EnablePreventFocusChange;
            this.Height = txtValue.Height;
            /////
            gr.MultiSelect = false;
            gr.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            gr.AllowUserToAddRows = false;
            gr.AllowUserToDeleteRows = false;
            gr.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            gr.ReadOnly = true;
            /////
            txtValue.Width = this.Width;
            //MaxHeight = 200;
            frm.Load += new EventHandler(frm_Load);
            this.gr.KeyDown += new System.Windows.Forms.KeyEventHandler(gr_KeyDown);
            this.gr.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(gr_CellDoubleClick);

        }
        void frm_Load(object sender, EventArgs e)
        {
            //frm.Left = this.Left + this.Left;
            //frm.Top = this.Top + this.Top;
            //frm.Location = new Point(this.Parent.Location.X + this.Location.X + 8, this.Parent.Location.Y + this.Location.Y + 50);
            frm.Location = this.PointToScreen(txtValue.Location);
            frm.Top += 22;

        }
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.txtValue.Width = this.Width;
        }

        private void TextBoxComplete_Load(object sender, EventArgs e)
        {

            //frm.Location = new Point(this.Parent.Location.X + this.Location.X, this.Parent.Location.Y + this.Location.Y);
            //frm = new Point(300, 300);
        }

        private void SelectedValue()
        {
            if (this.datasource.DefaultView.Count <= 0)
                return;
            if (Id.Trim() == "")
            {
                if (gr.SelectedRows.Count > 0)
                    value = gr.SelectedRows[0].Cells[0].Value;
                IsChange = false;
                txtValue.Text = value.ToString();
                IsChange = true;
            }
            else
            {
                if (gr.SelectedRows.Count > 0)
                    value = gr.SelectedRows[0].Cells[Id].Value;
                IsChange = false;
                txtValue.Text = value.ToString();
                IsChange = true;
            }
            DropDown = false;
        }

        public void CreateDataGrid()
        {
            //gr.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            if (this.datasource == null)
            {
                gr.DataSource = null;
            }
            else
                gr.DataSource = this.datasource.DefaultView;
            //gr.Width += 10;
            frm.Width = gr.Width;
            //gr.Width = frm.Width;
        }

        private void txtValue_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (IsChange)
                {
                    //frm.Show();
                    DropDown = true;
                    txtValue.Select(txtValue.Text.Length, 0);
                    txtValue.Focus();
                    if (FieldSearch.Trim() == "")
                        return;
                    string[] str = FieldSearch.Split(',', ';');
                    string condition = " 1 = 0 ";
                    foreach (string s in str)
                        condition += " OR " + s + " Like '%" + txtValue.Text + "%'";

                    this.datasource.DefaultView.RowFilter = condition;
                }
                //frm.Left += 100;
            }
            catch
            { }
        }

        private void txtValue_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                frm.Focus();
                DropDown = true;
                SendKeys.SendWait("{DOWN}");
                txtValue.Focus();
                txtValue.Select(txtValue.Text.Length, 0);
            }
            else
                if (e.KeyCode == Keys.Up)
                {
                    frm.Focus();
                    SendKeys.SendWait("{UP}");
                    txtValue.Focus();
                    txtValue.Select(txtValue.Text.Length, 0);
                }
                else
                    if (e.KeyCode == Keys.Enter)
                    {
                        SelectedValue();
                    }
        }

        private void txtValue_LostFocus(object sender, EventArgs e)
        {
            //System.Keyboard.FocusedElement.GetType();
            if (!frm.Focused && !txtValue.Focused && !gr.Focused)
                DropDown = false;

        }

        void TextBoxComplete_LostFocus(object sender, System.EventArgs e)
        {
            DropDown = false;
        }

        void gr_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.datasource.DefaultView.Count <= 0)
                    return;
                if (Id.Trim() == "")
                {
                    if (gr.SelectedRows.Count > 0)
                        value = gr.SelectedRows[0].Cells[0];
                    IsChange = false;
                    txtValue.Text = value.ToString();
                    IsChange = true;
                }
                else
                {
                    if (gr.SelectedRows.Count > 0)
                        value = gr.SelectedRows[0].Cells[Id];
                    IsChange = false;
                    txtValue.Text = value.ToString();
                    IsChange = true;
                }
                DropDown = false;
            }
        }

        void gr_CellDoubleClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            SelectedValue();
        }

        private void txtValue_Enter(object sender, EventArgs e)
        {
            DropDown = true;
            txtValue.Focus();
        }
    }
}
